# -*- coding:utf-8 -*-
from mq import BaseConsumer
from util.mail_sender import MailSender

class MailNotice(BaseConsumer):

    def init(self):
        self.mail = MailSender()
        self.mail.sender_name = '小惠出行'

    def _prepare_mail_txt(self, data):
        """
        配送通知

        :param data: {"product_name": "闪耀", "type": 1, "name": "name", "mobile": "", "province":"", "city":"",
        "address":"", "extraction_time": "", "loungehall":"", "create_date":""}
        :return:
        """
        html = u''
        # 配送区分，1：自提，2：配送
        if data.type == 1:
            html += u'<p><table id="tab" style="width:600px;" cellpadding="2" cellspacing="0" align="center" border="1" bordercolor="#000000">'
            html += u'<tbody><tr><td>ID</td>'
            html += u'<td style="text-align:center;font-size:14px;">姓名</td>'
            html += u'<td style="text-align:center;font-size:14px;">手机号</td>'
            html += u'<td style="text-align:center;font-size:14px;">自提点</td>'
            html += u'<td style="text-align:center;font-size:14px;">提取时间</td></tr>'

            html += u'<tr>'
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.id)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.name)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.mobile)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.loungehall)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.extraction_time)
            html += u'</tr>'

            html += u'</tbody></table></p>'
        else:
            html += u'<p><table id="tab" style="width:600px;" cellpadding="2" cellspacing="0" align="center" border="1" bordercolor="#000000">'
            html += u'<tbody><tr><td>ID</td>'
            html += u'<td style="text-align:center;font-size:14px;">姓名</td>'
            html += u'<td style="text-align:center;font-size:14px;">手机号</td>'
            html += u'<td style="text-align:center;font-size:14px;">省</td>'
            html += u'<td style="text-align:center;font-size:14px;">市</td>'
            html += u'<td style="text-align:center;font-size:14px;">详细地址</td>'
            html += u'</tr>'


            html += u'<tr>'
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.id)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.name)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.mobile)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.province)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.city)
            html += u'<td style="text-align:center;font-size:14px;">{}</td>'.format(data.address)
            html += u'</tr>'

            html += u'</tbody></table></p>'

        return html


    def runner(self, data):

        html = self._prepare_mail_txt(data)
        if data.type == 1:
            subject = u'空铁管家微商城配货通知(自提)-{}'.format(data.product_name)
        else:
            subject = u'空铁管家微商城配货通知(发货)-{}'.format(data.product_name)

        self.mail._send_mail(self.mail.receivers, subject, html)

        return True